package net.lucamasini.ricette.client.ricette;

import java.util.List;

import net.lucamasini.ricette.client.ClientFactory;
import net.lucamasini.ricette.client.ricette.RicetteView.Presenter;

import com.google.gwt.activity.shared.AbstractActivity;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.Place;
import com.google.gwt.requestfactory.shared.Receiver;
import com.google.gwt.user.client.ui.AcceptsOneWidget;

public class RicetteActivity extends AbstractActivity implements Presenter {

	private RicetteView ricetteView;
	private ClientFactory clientFactory;
	
	public RicetteActivity(ClientFactory clientFactory) {
		this.clientFactory = clientFactory;
		this.ricetteView = clientFactory.getRicetteView();
		this.ricetteView.setPresenter(this);
	}

	@Override
	public void start(final AcceptsOneWidget panel, EventBus eventBus) {
		clientFactory.getRequestFactory().ricetteRequest().findAllRicette().with("ingredientiRicetta").fire(new Receiver<List<RicettaProxy>>() {
			@Override
			public void onSuccess(List<RicettaProxy> response) {
				ricetteView.ricette().setRowCount(0);
				ricetteView.ricette().setRowData(0, response);
				ricetteView.unselectRicetteTable();
				panel.setWidget(ricetteView.asWidget());
			}
		});	
	}

	@Override
	public void goTo(Place place) {
		clientFactory.getPlaceController().goTo(place);
	}

	@Override
	public void remove(RicettaProxy ricetta) {
		clientFactory.getRequestFactory().ricetteRequest().remove(ricetta).fire();
	}
}
